(function() {
	function Slider(params) {
		this.defaultConfig = {
			dir: 'x',
			max: 100,
			min: 0,
			val: 0
		}
		this.defaultConfig = Object.assign({}, this.defaultConfig, params)
		console.log(this.defaultConfig);
		// 获取参数
		this.el = params.el
		this.dir = params.dir ? params.dir : 'x'
		this.max = params.max ? params.max : 100
		this.min = params.min ? params.min : 0
		this.val = params.val ? params.val : 0
		this.onMove = params.onMove
		// 					if(this.dir==='all'){
		// 						// 判断max,min,val,是数组
		// 					}

		this.el.style[this.dir === 'x' ? 'left' : 'top'] = this.val + 'px';
		// 设置基点
		this.orgX = 0;
		this.orgY = 0;
		this.init()

	}
	Slider.prototype.init = function() {
		var _this = this;
		_this.el.addEventListener('mousedown', function(e) {
			var e = e || window.event
			_this.orgX = e.clientX - _this.el.offsetLeft
			_this.orgY = e.clientY - _this.el.offsetTop
			_this.el.style.webkitTransition = _this.el.style.transition = 'none'
			e.preventDefault()
			document.onmousemove = function(e) {
				_this.touchMove(e)
			}
			document.onmouseup = function(e) {
				_this.touchEnd(e)
			}
			return false
		})
	}
	Slider.prototype.touchMove = function(e) {
		var e = e || window.event
		// 判断方向
		if (this.dir === 'x') {
			this.val = e.clientX - this.orgX
		} else if (this.dir === 'y') {
			this.val = e.clientY - this.orgY
		} else if (this.dir === 'all') {
			this.val = [e.clientX - this.orgX, e.clientY - this.orgY]
		}
		// 限定范围
		if (this.dir === 'all') {
			if (this.val[0] < this.min[0]) {
				this.val[0] = this.min[0]
			}
			if (this.val[0] > this.max[0]) {
				this.val[0] = this.max[0]
			}
			if (this.val[1] < this.min[1]) {

				this.val[1] = this.min[1]
				console.log(this.val[1]);
			}
			if (this.val[1] > this.max[1]) {
				this.val[1] = this.max[1]
			}
			this.el.style.left = this.val[0] + 'px'
			this.el.style.top = this.val[1] + 'px'
		} else {
			// x,y
			// 判断最大最小值  范围限定
			if (this.val < this.min) {
				this.val = this.min
			} else if (this.val > this.max) {
				this.val = this.max
			}
			this.el.style[this.dir === 'x' ? 'left' : 'top'] = this.val + 'px'
		}
		// 返回val值
		if (typeof this.onMove === 'function') this.onMove(this.val)
		e.preventDefault()

	}
	Slider.prototype.touchEnd = function(e) {
		document.onmousemove = document.onmouseup = null
	}
	// 设置值
	Slider.prototype.setValue = function(val) {
		if (this.dir === 'all') {
			if (isArray(val)) {
				this.val = val
				this.el.style.left = this.val[0] + 'px'
				this.el.style.top = this.val[1] + 'px'
			} else {
				console.log('val:数据类型错误，请输入数组类型');
			}
			return
		}
		this.val = val
		this.el.style[this.dir === 'x' ? 'left' : 'top'] = this.val + 'px'
	}
	// 判断是否为数组
	function isArray(arg) {
		if (typeof arg === 'object') {
			if (arg instanceof Array) {
				return true
			}
			return false
		} else {
			return false
		}
	}
	window.Slider = Slider
})();
